<?php require_once('connections/con_gl.php'); require_once("excel/excelwriter.class.php");?>

<?php

cekAkses($_SESSION[akses],"L-3");

echo $_SESSION[nama];

mysql_select_db($database_con_gl, $con_gl);

mysql_query("delete from gl_trans where total='0'", $con_gl);

$currentPage = $_SERVER["PHP_SELF"];



$maxRows_data = 20;

$pageNum_data = 0;

if (isset($_GET['pageNum_data'])) {

  $pageNum_data = $_GET['pageNum_data'];

}

$startRow_data = $pageNum_data * $maxRows_data;

$cr_txt = str_replace("+"," ",$_REQUEST[cr_txt]);

mysql_select_db($database_con_gl, $con_gl);

$query_data = "SELECT * from v_kasir a inner join v_kendaraan b on a.kendaraan = b.nopolisi";
$query_korporate = "SELECT * from v_kasir_korporate a inner join v_kendaraan b on a.kendaraan = b.nopolisi";

/*
if(!strstr($_SESSION[akses],'A-1-4')) {

	$query_data .= " AND gudang='$_SESSION[gudang]'";

}
*/
if($_GET[gudang]<>'ALL' AND $_GET[gudang]<>'') { 

$query_data .=" where a.gudang='$_GET[gudang]'";
$query_korporate  .=" where a.namagudang='$_GET[gudang]'";

}
else{
$query_data .=" where a.gudang='$_SESSION[outlet]'";
$query_korporate  .=" where a.namagudang='$_SESSION[outlet]'";
}
if($_GET[cr_txt]<>'' AND $_GET[cr_txt]<>'+') { 

$decode_cr = str_replace("+"," ",$_GET[cr_txt]);

$query_data .=" AND (a.no_ref LIKE '%%$decode_cr%%' OR a.nama LIKE '%%$decode_cr%%' OR a.paket LIKE '%%$decode_cr%%')";
$query_korporate .=" AND (a.no_ref LIKE '%%$decode_cr%%' OR a.kontrak LIKE '%%$decode_cr%%')";

}



if($_GET[mulai]<>'' AND $_GET[sampai]<>'') { 

$query_data .=" AND a.checkin between '$_GET[mulai]' AND '$_GET[sampai]'";
$query_korporate .=" AND a.mulai between '$_GET[mulai]' AND '$_GET[sampai]'";
}

$query_data .=" ORDER BY a.checkin desc";
//print_r($query_data);
$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$totalRows_data = mysql_num_rows($data);

$totalPages_data = floor($totalRows_data / $maxRows_data);

$query_data .=" LIMIT $startRow_data, $maxRows_data";
$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$totalRow_data = mysql_num_rows($data);
$row_data = mysql_fetch_assoc($data);



$queryString_data = "";

if (!empty($_SERVER['QUERY_STRING'])) {

  $params = explode("&", $_SERVER['QUERY_STRING']);

  $newParams = array();

  foreach ($params as $param) {

    if (stristr($param, "pageNum_data") == false && 

        stristr($param, "totalRows_data") == false) {

      array_push($newParams, $param);

    }

  }

  if (count($newParams) != 0) {

    $queryString_data = "&" . htmlentities(implode("&", $newParams));

  }

}

$queryString_data = sprintf("&totalRows_data=%d%s", $totalRows_data, $queryString_data);



// Export Excel

$excel=new ExcelWriter("report_penjualan.xls");

if($excel==false)	

echo $excel->error;



$myArr=array("Tanggal","No Referensi","Gudang","Total","Keterangan");

$excel->writeLine($myArr);



$qry=mysql_query($query_data, $con_gl);

if($qry!=false)

{

	$i=1;

	while($res=mysql_fetch_array($qry))

	{

		$total = mysql_query("SELECT sum(gl_trans.total) as total FROM gl_trans WHERE gl_trans.no_ref='$row_data[no_ref]' AND pos='K'",$con_gl);

		$row_total = mysql_fetch_assoc($total);

		$myArr=array($res['tgl'],$res['no_ref'],$res['gudang'],$row_total['total'],$res['keterangan']);

		$excel->writeLine($myArr);

		$i++;

	}

}


// End Export

include('include/widget_export_trans.php');

?>

<link href="css/ui.datepicker.css" type="text/css" rel="stylesheet" />

<script language="javascript">

function download()

{

	window.location='report_penjualan.xls';

}

</script>

<script type="text/javascript" src="js/ui.datepicker.js"></script>

<script type="text/javascript">

<!--

$(function()

      {

        $('.calendar').datepicker({

            appendText : "",

            dateFormat : 'yy/mm/dd'

          });

      });

</script>

<script type="text/javascript">

$(document).ready(function(){

$(".flipsearch").click(function(){

	$("form.tanggalsearch").hide() 

    $(".objsearch").slideToggle("fast");

  });

});



$(document).ready(function(){

$(".fliptanggal").click(function(){

	$("form.objsearch").hide();

    $(".tanggalsearch").slideToggle("fast");

  });

});

</script>

<h1>Daftar Transaksi Order</h1>

<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable"> 

  <tr valign="top" bgcolor="#FFFFFF" class="hide">

    <td> Pencarian</td>

    <td colspan="11" align="left"><?php

	mysql_select_db($database_con_gl, $con_gl);

$query_cr_gud = "SELECT id, gudang FROM gl_gudang";

if(!strstr($_SESSION[akses],'A-1-4')) {

	$query_cr_gud .= " where id='$_SESSION[lokasi]' ";

}

$query_cr_gud .= " ORDER BY gudang ASC";

$cr_gud = mysql_query($query_cr_gud, $con_gl) or die(mysql_error());

$row_cr_gud = mysql_fetch_assoc($cr_gud);

$totalRows_cr_gud = mysql_num_rows($cr_gud);

?>

	<form id="search" name="search" method="post" action="">

<label><select name="cr_gudang">

	      <option value="ALL">Pilih Cabang</option>

	      <?php

do {  

?>

	      <option value="<?php echo $row_cr_gud['gudang']?>" <?php if($_GET['gudang']==$row_cr_gud['gudang']) echo "selected"; ?>><?php echo $row_cr_gud['gudang']?></option>

	      <?php

} while ($row_cr_gud = mysql_fetch_assoc($cr_gud));

  $rows = mysql_num_rows($cr_gud);

  if($rows > 0) {

      mysql_data_seek($cr_gud, 0);

	  $row_cr_gud = mysql_fetch_assoc($cr_gud);

  }

?>

        </select></label>

	    <label>

      Tanggal : <input name="mulai" type="text" id="mulai" value="<?php echo $_GET[mulai];?>" size="15" maxlength="15" class="calendar"/></label> s/d <label><input name="sampai" type="text" id="sampai" value="<?php echo $_GET[sampai];?>" size="15" maxlength="15" class="calendar"/>

        </label>

		<label>

      Kata Kunci : <input name="cr_txt" type="text" id="cr_txt" placeholder="Find by: no.referensi,nama,paket"; onclick="this.value='';"<?php echo $_REQUEST['cr_txt'];?>" size="40" maxlength="100"/>
        </label>

        <label>

      <input name="Search" type="submit" id="Search" value="Search" />
        </label>
	  </form>

	<?php

	if(isset($_POST['Search'])) {

		echo "<script>window.location=\"index.php?component=sretail&mulai=".$_POST[mulai]."&sampai=".$_POST[sampai]."&cr_txt=".str_replace(" ","+",$_POST[cr_txt])."&gudang=".$_POST[cr_gudang]."\";</script>";

	}

	?></td>
  </tr>

  <tr>

    <th width="78"><strong>Berangkat</strong></th>
    <th width="78"><strong>No Order</strong></th>
	<th width="78">Area </th>
   	<th width="78">Lokasi </th>
    <th width="78">Paket</th> 
    <th width="78">No. Polisi </th>
	<th width="78">Customer </th>    
    <th width="78" align="center"><strong>T.Sewa</strong></th>
    <th width="78" align="center"><strong>T.Bayar</strong></th>
    <th width="78" align="center"><strong>Piutang</strong></th>
    <th width="60" align="center"><strong>Status</strong></th>
    <th width="200" align="center" class="hide"><strong>Aksi</strong></th>
  </tr>

  <?php if($totalRows_data > 0) { 
  		$tpiutang = 0;
	    $ttotal = 0;
		$tbayar = 0;?>
  	  

  <?php do { ?>

  <?php

mysql_select_db($database_con_gl, $con_gl);

$query_hitung = "SELECT sum(gl_trans.total) FROM gl_trans WHERE gl_trans.no_ref='$row_data[no_ref]' AND pos='K'";

$hitung = mysql_query($query_hitung, $con_gl) or die(mysql_error());

$row_hitung = mysql_fetch_assoc($hitung);

$totalRows_hitung = mysql_num_rows($hitung);

  ?>

    <tr valign="top" bgcolor="#FFFFFF">

      <td align="left"><?php tanggal($row_data['checkin'],"tampilkan");?></td>
<td align="left"><a href="include/jurnalvoucher_transaksi.php?referensi=<?php echo $row_data['no_ref'];?>" onclick="NewWindow(this.href,'name','1050','300','yes');return false" title="Jurnal Voucher"><?php echo $row_data['no_ref']; ?></a></td>
      <!--<td align="left"><?php echo $row_data['no_ref']; ?></a></td>-->
	   <td><?php echo $row_data['area']; ?></td>
       <td><?php echo $row_data['gudang']; ?></td>
       <td><?php echo $row_data['paket']; ?></td>
       <td><?php echo $row_data['kendaraan']; ?></td>
       <td><?php echo $row_data['nama']; ?></td>      
      <td><?php $ttotal += $row_data['totalsewa'];echo number_format($row_data['totalsewa'],0,',','.').",-"; ?></td>
      <td><?php $tbayar += $row_data['totalbayar'];echo number_format($row_data['totalbayar'],0,',','.').",-"; ?></td>
       <td><?php $tpiutang += $row_data['sisakekurangan'];echo number_format($row_data['sisakekurangan'],0,',','.').",-"; ?></td>
       <td><?php echo $row_data['status']; ?></td>

      <td align="center" bgcolor="#FFFFFF" class="hide"><a href="include/invoice.php?referensi=<?php echo $row_data['no_ref'];?>" onclick="NewWindow(this.href,'name','735','450','yes');return false" title="View Invoice"><img src="images/invoices.png" border="0" /></a> <a href="include/biaya_oprs.php?referensi=<?php echo $row_data['no_ref'];?>" onclick="NewWindow(this.href,'name','935','500','yes');return false" title="View Biaya Ops"><img src="images/invoices.png" border="0" /></a><a href="include/fakturpajak.php?referensi=<?php echo $row_data['no_ref'];?>" onclick="NewWindow(this.href,'name','765','500','yes');return false" title="View Faktur Pajak"><img src="images/tax.png" border="0" /></a> </a><a href="include/kwitansi.php?referensi=<?php echo $row_data['no_ref'];?>" onclick="NewWindow(this.href,'name','935','500','yes');return false" title="View Kwintasi"><img src="images/invoices.png" border="0" /></a><a href="index.php?component=sretail&amp;task=edit&amp;noreferensi=<?php echo $row_data[no_ref];?>"><img src="images/edit_.png" border="0" /></a> <?php if(strstr($_SESSION['akses'],"L-3")) { ?><a href="index.php?component=sretail&amp;task=bayar&amp;noreferensi=<?php echo $row_data[no_ref];?>+&open=window" onclick="NewWindow(this.href,'name','1300','600','yes');return false"><img src="images/transactions.png" border="0" />Bayar</a><?php } ?> <?php if(strstr($_SESSION['akses'],"L-3")) { ?><?php } ?></td>
    </tr>

    <?php } while ($row_data = mysql_fetch_assoc($data)); ?>
     <tr bgcolor="red">
     <td colspan="7" width="146"><strong>Total</strong></th>
      <td width="78" align="center"><strong><?php echo number_format($ttotal,0,',','.').",-";?></strong></th>
       <td width="78" align="center"><strong><?php echo number_format($tbayar,0,',','.').",-";?></strong></th>
        <td width="78" align="center"><strong><?php echo number_format($tpiutang,0,',','.').",-";?></strong></th>
     </tr>
	<?php if($totalRows_data > $maxRows_data) { ?>
    <tr class="hide">
      <td colspan="12"><table border="0" align="left" width="50%">
          <tr>
            <td width="3%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, 0, $queryString_data); ?>"><img src="First.gif" border=0 ></a>
                <?php } // Show if not first page ?>            </td>
            <td width="3%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, max(0, $pageNum_data - 1), $queryString_data); ?>"><img src="Previous.gif" border=0></a>
                <?php } // Show if not first page ?>            </td>
            <td width="3%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, min($totalPages_data, $pageNum_data + 1), $queryString_data); ?>"><img src="Next.gif" border=0></a>
                <?php } // Show if not last page ?>            </td>
            <td width="3%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, $totalPages_data, $queryString_data); ?>"><img src="Last.gif" border=0></a>
                <?php } // Show if not last page ?>            </td>
			<td width="88%">Halaman : <?php echo $pageNum_data+1; ?>, Ditampilkan <?php echo $totalRow_data; ?> dari <?php echo $totalRows_data; ?> total data</td>
          </tr>
        </table></td>
    </tr>
	<?php } ?>
   <?php }else{ ?>

  <tr bgcolor="#FFFFFF">

    <td colspan="8">Data tidak ada !!! </td>
  </tr>

  <?php } ?>

  <tr class="hide">

    <td colspan="8"><?php if(strstr($_SESSION['akses'],"L-3")) { ?><?php } ?></td>
  </tr>
</table>

<?php
		

mysql_free_result($cr_gud);

?>

